<?php

namespace App\Models;

use DateTimeInterface;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class BaseModel extends Model
{

    const PAGE_NO = 1;
    const PAGE_SIZE = 10;
    const PREFIX = 'xt_';

    ###############     默认参数      #################

    public $timestamps = false;
    protected static $defaultField = '*';
    protected static $listField = '';
    protected static $infoField = '';

    ##################################################

    public function __construct()
    {
        parent::__construct();
    }

    // protected static function redis(array $config=[])
    // {
    //     $instance = \EasySwoole\EasySwoole\Config::getInstance();
    //     $config = !empty($config) ? $config : $instance->getConf('REDIS');
    //     $redis = new \EasySwoole\Redis\Redis(new \EasySwoole\Redis\Config\RedisConfig($config));
    //     return $redis;
    // }


    // 2020-04-08 18:57:59  设置日期格式化,否则在使用toArray时日期格式不对
    protected function serializeDate(DateTimeInterface $date)
    {
        return strtotime($date->format('Y-m-d H:i:s'));
    }

    /**
     * 验证分页数据
     */
    protected static function checkPageInfo(&$pageNo, &$pageSize)
    {
        if ($pageNo <= 0) $pageNo = self::PAGE_NO;
        if ($pageSize <= 0) $pageSize = self::PAGE_SIZE;
    }

    /**
     * 生成当前分页信息
     */
    protected static function createPageInfo($count, $pageNo, $pageSize)
    {
        return [  
            'count'         => $count,
            'pages'         => ceil($count/$pageSize),
            'current_page'  => $pageNo,
            'size'          => $pageSize
        ];
    }

    /**
     * 生成列表分页信息
     */
    protected static function createListInfo($list, $count, $pageNo, $pageSize)
    {
        $pageInfo = [  
            'count'         => $count,
            'pages'         => ceil($count/$pageSize),
            'current_page'  => $pageNo,
            'size'          => $pageSize
        ];
        return ['list' => $list, 'page_info' => $pageInfo];
    }

    /**
     * 详情
     */
    public function info(array $where)
    {
        $result = $this -> selectRaw(!empty(static::$infoField) ? static::$infoField : static::$defaultField) -> where($where) -> first();
        if ($result) return $result -> toArray();
        return null;
    }

    
    /**
     * 分页列表
     */
    public function list(array $param) : ? array
    {
        $pageNo = $param['page_no'] ?? 0;
        $pageSize = $param['page_size'] ?? 0;
        self::checkPageInfo($pageNo, $pageSize);

        $where = [];
        $model = $this -> where($where);

        $count = $model -> count();
        if (!$count) return null;
        
        $list = $model 
            -> select(DB::raw(!empty(static::$listField) ? static::$listField : static::$defaultField)) 
            -> offset(($pageNo - 1) * $pageSize) -> limit($pageSize)
            -> get() -> toArray();
        
        if (!$list) return null;
        
        return self::createListInfo($list, $count, $pageNo, $pageSize);
    }

    /**
     * 所有列表
     */
    public function allList($param) : ? array
    {
        $field = $param['field'] ?? (!empty(static::$listField) ? static::$listField : static::$defaultField);
        $where = $param['where'] ?? [];
        $list = $this -> select(DB::raw($field)) -> where($where) -> get() -> toArray();
        return $list ?? null;
    }

}